package org.tc.feign.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tc.client.entity.Users;
import org.tc.feign.api.Dx;
import org.tc.feign.client.service.FeignUsersService;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin(origins = {"*"},methods={RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
@RestController
public class FeignUsersController {

    @Autowired
    private FeignUsersService feignUsersService;

    @RequestMapping("/test")
    public String Test(){
        return "测试";
    }

    @RequestMapping("/selectTestUsers")
    public Object selectUsers(Integer id){
        System.out.println("fegin_____________----");
        System.out.println(id);
        return feignUsersService.selectUser(id);
    }


    /**
     * 添加手机号 如果已经有该用户 则修改会员为是
     * @param users
     * @return
     */
    @RequestMapping("/insertByMem")
    public int insertByMem(@RequestBody Users users){
        return feignUsersService.insertBuMemNum(users);
    }

    /**
     * 根据会员号修改会员积分
     * @param user
     * @return
     */
    @RequestMapping("/updateScoreByMemNumber")
    public int updateScoreByMemNumber(@RequestBody Users user) {
        return feignUsersService.updateScoreByMemNumber(user);
    }

    /**
     * 注册用户信息
     * @param users
     * @return
     */

    @RequestMapping("/insertUser")
    public int insertUser(@RequestBody Users users){
        System.out.println(users.getNickname());
        System.out.println("注册");
        return feignUsersService.insertUser(users);
    }


    /**
     * 根据手机号查询或者昵称查询
     * @param phone
     * @param nickname
     * @return
     */
    @RequestMapping("/selectByPhone")
    @ResponseBody
    public Users selectByPhone(@RequestParam(value="phone",required = true) String phone,@RequestParam(value = "nickname",required = false)String nickname){
        System.out.println("根据手机号查询");
        return feignUsersService.selectByPhone(phone,nickname);
    }

    /**
     * 根据昵称修改用户信息
     * @param users
     * @return
     */
    @RequestMapping("/updateUsersById")
    public int updateUsersById(@RequestBody Users users){
        System.out.println("根据id修改个人信息");
        return feignUsersService.updateUsersById(users);
    }

    @RequestMapping("/loginUser")
    public Users login(String phone, String pwd,HttpServletRequest request){
        Users users=feignUsersService.login(phone,pwd);
        if (users!=null){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.set("userid",users.getId().toString());
            jedis.set("phone",users.getPhone());
            jedis.set("Nickname",users.getNickname());
            jedis.set("Member",users.getMember().toString());

        }
        return users;
    }
    //退出登陆
    @RequestMapping("/tc")
    public int tc(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.del("userid");
        jedis.del("phone");
        jedis.del("Nickname");
        jedis.del("Member");
        return 1;
    }


    //session存用户信息
    @RequestMapping(value = "/selUser",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public Map<String, String> selUser(HttpServletRequest request){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Map<String,String> map = new HashMap<>();
        map.put("userid",jedis.get("userid"));
        map.put("phone",jedis.get("phone"));
        map.put("Nickname",jedis.get("Nickname"));
        map.put("Member",jedis.get("Member"));
        return map;
    }


    @RequestMapping("/servlet")
    @ResponseBody
    public void servlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        String phone = request.getParameter("phone");
        System.out.println("phone===="+phone);
        Dx dx = new Dx();
        String xym = dx.string(phone);
        System.out.println("发送的验证码====="+xym);
        out.print(xym);
    }
}
